package parser

import (
	"learngo/crawler/config"
	"learngo/crawler/engine"
	"regexp"
)

var profileRe = regexp.MustCompile(`<a href="(http://album.zhenai.com/u/[0-9]+)" [^>]*>([^<]+)</a>`)
var cityURLRe = regexp.MustCompile(`href="(http://www.zhenai.com/zhenghun/[^"]+)"`)


func ParserCity(contents []byte, _ string) engine.ParserResult{
	matches := profileRe.FindAllSubmatch(contents, -1)

	result := engine.ParserResult{}
	for _,c := range matches{
		result.Requests = append(result.Requests,engine.Request{
			Url: string(c[1]),
			Parser: NewProfileParser(
				string(c[2])),
		})
	}

	matches = cityURLRe.FindAllSubmatch(contents, -1)
	for _,c := range matches{
		//fmt.Printf("match = %s\n",c)
		result.Requests = append(result.Requests,engine.Request{
			Url: string(c[1]),
			Parser: engine.NewFuncParser(
				ParserCity, config.ParseCity),
		})
	}
	return result
}
